package com.gfeeling.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gfeeling.model.UploadCount;
import com.gfeeling.service.LogService;
import com.gfeeling.service.UploadCountService;



@Controller
@RequestMapping("/test")
public class TestController extends BaseController{
	@Autowired
	private UploadCountService uploadCountService;
	@Autowired
	private LogService logService;
	@RequestMapping("/test")
    public void  test(HttpServletResponse response) throws Exception{
    	boolean r = true ;
		String msg = "登录名或密码不正确!";
    	write(String.format("{\"r\":%s,\"m\":\"%s\"}", r , msg), response);
    }
	@RequestMapping("/druid")
	public String druid() {
		return "redirect:/druid/index.html";
	}
	@RequestMapping("/upload")
	public void upload(HttpServletRequest request,HttpServletResponse response) throws Exception{
		File file = new File("D:\\logs.zip");
		String name = "文件.zip";
		fileDownload(response,file,name);
		UploadCount uploadCount = new UploadCount();
		uploadCount.setCreateTime(new Date());
		uploadCount.setIp(request.getRemoteAddr());
		uploadCountService.save(uploadCount);
	}
	@RequestMapping("/count")
	public void count(HttpServletResponse response) throws Exception{
		Long count = uploadCountService.countHQL(new UploadCount());
		boolean r = true ;
    	write(String.format("{\"r\":%s,\"m\":\"%s\"}", r , count), response);
	}
	@RequestMapping("/count2")
	public @ResponseBody  Map<String, Object> count2(HttpServletResponse response) throws Exception{
		Long count = uploadCountService.countHQL(new UploadCount());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("r", true);
		map.put("count", count);
    	return map;
	}
	@RequestMapping("/log")
	public @ResponseBody  Map<String, Object> log(HttpServletResponse response) throws Exception{
		int count = logService.delLogForDate("2016-06-30");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("r", true);
		map.put("count", count);
		System.out.println(pageSize);
		map.put("pageSize", pageSize);
    	return map;
	}
}
